package com.todo.user.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.user.infrastructure.entity.condition.UserAuthCondition;
import com.todo.user.infrastructure.entity.po.UserAuthPO;
import com.todo.user.infrastructure.mapper.UserAuthMapper;
import com.todo.user.infrastructure.repository.UserAuthRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserAuth)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("userAuthRepository")
public class UserAuthRepositoryImpl implements UserAuthRepository {
    @Resource
    private UserAuthMapper userAuthMapper;


    @Override
    public Boolean removeUserAuthPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return userAuthMapper.removeUserAuthPOById(id) > 0;
    }

    @Override
    public UserAuthPO queryUserAuthPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return userAuthMapper.queryUserAuthPOById(id);
    }

    @Override
    public PageResult<UserAuthPO> queryUserAuthPOByCondition(UserAuthCondition userAuthCondition) {
        PageResult<UserAuthPO> pageResult = new PageResult<>();
        if (userAuthCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(userAuthCondition.getPageIndex());
        pageResult.setPageSize(userAuthCondition.getPageSize());
        long count = queryCountByCondition(userAuthCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, userAuthCondition.getPageSize()));
        pageResult.setTotal(count);
        List<UserAuthPO> userAuthPOList = userAuthMapper.queryUserAuthByCondition(userAuthCondition);
        if (CollectionUtils.isEmpty(userAuthPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(userAuthPOList);
        pageResult.setCurrentIndex(userAuthPOList.get(0).getId());
        pageResult.setHasNext(userAuthPOList.size() >= userAuthCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(UserAuthCondition userAuthCondition) {
        if (userAuthCondition == null || userAuthCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return userAuthMapper.queryCountByCondition(userAuthCondition);
    }

    @Override
    public Boolean insertUserAuthPO(UserAuthPO userAuthPO) {
        if (userAuthPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userAuthPO)) {
            return false;
        }
        return userAuthMapper.insertUserAuthPO(userAuthPO) > 0;
    }

    @Override
    public Boolean updateUserAuthPOById(UserAuthPO userAuthPO) {
        if (userAuthPO == null || userAuthPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userAuthPO)) {
            return false;
        }
        return userAuthMapper.updateUserAuthPOById(userAuthPO) > 0;
    }


    @Override
    public Long removeUserAuthPOByUserId(Long userId) {
        if (userId == null || userId < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return userAuthMapper.removeUserAuthPOByUserId(userId);
    }

    @Override
    public List<UserAuthPO> queryUserAuthPOByUserId(Long userId) {
        if (userId == null || userId < 0) {
            return Lists.newArrayList();
        }
        return userAuthMapper.queryUserAuthPOByUserId(userId);
    }


    private boolean baseFieldCheck(UserAuthPO userAuthPO) {
        if (userAuthPO == null) {
            return false;
        }
        if (userAuthPO.getUserId() == null || userAuthPO.getUnionId() == null || userAuthPO.getOpenId() == null) {
            log.warn("UserAuthRepository#baseFieldCheck 核心字段校验失败，操作失败 userAuthPO：【{}】", JSONObject.toJSONString(userAuthPO));
            return false;
        }
        if (userAuthPO.getPlatformKey() == null || userAuthPO.getPlatformChannelKey() == null || userAuthPO.getBindStatus() == null) {
            log.warn("UserAuthRepository#baseFieldCheck 基础字段校验失败，操作失败 userAuthPO：【{}】", JSONObject.toJSONString(userAuthPO));
            return false;
        }
        return true;
    }
}
